package com.ruipengkj.view.memberView;
/**
 * 说明：
 * 添加会员级别界面
 * @Author：孙航
 * @Date：2012-03-20
 */
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.bean.TShopMemberLevel;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.UUID;
import com.ruipengkj.component.JLabelJComboBoxComponent;
import com.ruipengkj.component.JLabelJTextAreaComponent;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.service.TShopMemberLevelService;
import com.ruipengkj.view.base.BaseDialog;


public class MemberLevelDialog extends BaseDialog {

	private static final long serialVersionUID = 1L;

	private JLabelJTextFieldComponent levelID;
	private JLabelJTextFieldComponent name;
	private JLabelJComboBoxComponent isDiscount;
	private JLabelJTextFieldComponent discount;

	private JLabelJTextFieldComponent walve;
	private JLabelJTextFieldComponent level;
	private JLabelJComboBoxComponent walveType;
	private JLabelJTextAreaComponent des;
	
	private JButton sureButton;
	private JButton cancelButton;
	
	private TShopMemberLevel tShopMemberLevel = null;
	private TShopMemberLevelService tMemberLevelService;
//	private TEmployeeService tEmployeeService;//
	public boolean isSuccess=false;//判断是否添加成功
	public MemberLevelDialog(JFrame jf,String title,boolean b) {
		super(jf, title, b);
		tMemberLevelService=BuildSpringApplication.getInstance().getTShopMemberLevelServiceImpl();
		this.setSize(600, 400);
		this.init();
		this.initListener();
		this.setLocationRelativeTo(null);
	}
	
	public void init() {
		JPanel main_jp=new JPanel();
		BoxLayout boxLayout=new BoxLayout(main_jp,BoxLayout.Y_AXIS);
		main_jp.setLayout(boxLayout);
		
		levelID= new JLabelJTextFieldComponent("  *", "级别编号：", 30);
		name = new JLabelJTextFieldComponent("  *", "级别名称：", 30);
		isDiscount = new JLabelJComboBoxComponent("是否有折扣：", ApplicationContext.MEMBERLEVEL_ISDISCOUNT_MAP(), 100);
		discount= new JLabelJTextFieldComponent("*", "折扣：", 15);
		walve = new JLabelJTextFieldComponent("*", "阀值：", 15);
		level = new JLabelJTextFieldComponent("*", "级别：", 15);
		walveType = new JLabelJComboBoxComponent("阀值类型：", ApplicationContext.MEMBERLEVEL_WALUETYPE_MAP(), 100);
		des = new JLabelJTextAreaComponent("会员权限描述：",30 ,50);
		
		levelID.getText().setText(UUID.GenUUID15());
		levelID.getText().setEditable(false);
		
		main_jp.add(levelID);	
		main_jp.add(name);
		main_jp.add(isDiscount);
		main_jp.add(discount);
		main_jp.add(walve);
		main_jp.add(level);
		main_jp.add(walveType);
		main_jp.add(des);
		
		JPanel p1 = new JPanel();
		sureButton=new JButton("保存(Ctrl+S)");
		cancelButton=new JButton("取消(Ctrl+Q)");
		p1.add(sureButton);
		p1.add(cancelButton);
		main_jp.add(p1);
		this.add(main_jp);
	}

	private void initListener(){
		sureButton.addActionListener(this);
		sureButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		cancelButton.addActionListener(this);
		cancelButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_Q, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == sureButton) {
			String levelIDStr=levelID.getText().getText().trim();
			String nameStr=name.getText().getText().trim();
			String isDiscountStr=(String) isDiscount.getBox().getSelectedItem();
			String discountStr=discount.getText().getText().trim();
			String walveStr=walve.getText().getText().trim();
			String levelStr=level.getText().getText().trim();
			String walveTypeStr=(String) walveType.getBox().getSelectedItem();
			System.out.println(isDiscountStr);
			String desStr=des.getArea().getText();
			if(nameStr.equals("")){
				JOptionPane.showMessageDialog(null, "级别名称不能为空");
				name.getText().requestFocus();
				return;
			}
			
			if(isDiscountStr.equals("是")){
				if(discountStr.equals("")){
					JOptionPane.showMessageDialog(null, "折扣不能为空");
					discount.getText().requestFocus();
					return;
				}
			}
			if(isDiscountStr.equals("否")){
				discount.getText().setEditable(false);
			}
			
			if(levelStr.equals("")){
				JOptionPane.showMessageDialog(null, "级别不能为空");
				level.getText().requestFocus();
				return;
			}
			if(walveStr.equals("")){
				JOptionPane.showMessageDialog(null, "阀值不能为空");
				walve.getText().requestFocus();
				return;
			}
			
			
			boolean isOpLog=false;
			if(tShopMemberLevel==null){
				tShopMemberLevel = new TShopMemberLevel();
				isOpLog=true;
			}
			tShopMemberLevel.setLevelID(levelIDStr);
			tShopMemberLevel.setName(nameStr);
			if(isDiscountStr.equals("是")){
				tShopMemberLevel.setIsDiscount(true);
			}else{
				tShopMemberLevel.setIsDiscount(false);
			}	
			tShopMemberLevel.setDiscount(Double.parseDouble(discountStr));
			tShopMemberLevel.setWalve(Byte.parseByte(walveStr));
			if(walveTypeStr.equals("消费积分")){
				tShopMemberLevel.setWalveType((byte) 1);
			}else if(walveTypeStr.equals("充值积分")){
				tShopMemberLevel.setWalveType((byte) 2);
			}else{
				tShopMemberLevel.setWalveType((byte) 3);
			}
			
			tShopMemberLevel.setLevel(Integer.parseInt(levelStr));
			tShopMemberLevel.setType(ApplicationContext.TYPE_CUSTOM);
			tShopMemberLevel.setDes(desStr);
//			tStore.setManager(bean.getStrId());
			try {
				this.tMemberLevelService.saveOrUpdateService(tShopMemberLevel);
				this.isSuccess=true;
				if(isOpLog){
					BuildSpringApplication.getInstance().getTOpLogServiceImpl().addOpLogService("添加会员级别信息");
				}else{
					BuildSpringApplication.getInstance().getTOpLogServiceImpl().addOpLogService("修改会员级别信息");	
				}
				JOptionPane.showMessageDialog(null, "操作成功");
			} catch (Exception e1) {
				e1.printStackTrace();
				this.isSuccess=false;
				JOptionPane.showMessageDialog(null, "操作失败");
			}
			this.dispose();
		} else if(e.getSource() == cancelButton) {
			this.isSuccess=false;
			this.dispose();
		}
	}
	
	public void showDialog(TShopMemberLevel tShopMemberLevel){
		this.isSuccess=false;
		if(tShopMemberLevel!=null){
			this.tShopMemberLevel=tShopMemberLevel;
			levelID.getText().setText(tShopMemberLevel.getLevelID());
			name .getText().setText(tShopMemberLevel.getName());
			isDiscount.getBox().setSelectedItem(tShopMemberLevel.isIsDiscount());
			discount.getText().setText(Double.toString(tShopMemberLevel.getDiscount()));
			walve.getText().setText(tShopMemberLevel.getWalve()+"");
			level.getText().setText(Integer.toString(tShopMemberLevel.getLevel()));
			walveType.getBox().setSelectedItem(tShopMemberLevel.getWalveType());
			des.getArea().setText(tShopMemberLevel.getDes());
		}
		this.setVisible(true);
	}

//	public Runnable queryEmp=new Runnable() {
//		@Override
//		public void run() {
//			try {
////				List<TEmployee> list=tEmployeeService.findService(null);
//				if(waluetype!=null&&waluetype.size()!=0){
//					for(int i=0;i<waluetype.size();i++){
////						TEmployee emp=list.get(i);
//						manager.addElement(new IDNameBean(emp.getEmployeeID(), emp.getName()));
//						if(tStore!=null){
//							if(emp.getEmployeeID().equals(tStore.getManager())){
//								manager.getBox().setSelectedIndex(i+1);
//							}
//						}
//					}
//				}
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//		}
////	};
}
